<template>
  <el-dialog
    :append-to-body="true"
    @close="closeDialog"
    :visible.sync="dialog"
    :title="is_edit?'编辑报警类型':'添加报警类型'"
    width="500px"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="报警类型" prop="name">
        <el-select v-model="form.name" placeholder="请选择" style="width: 320px;">
          <el-option
            v-for="item in types"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报警值" prop="value">
        <el-input placeholder="输入报警值" v-model="form.value" style="width: 320px;" maxlength="10" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" size="mini" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { _reportCreate, _reportUpdate } from "@/http/ajax";
export default {
  name: "edit",
  props: {
    is_edit: {
      type: Boolean,
      required: true
    },
    cur_this: {
      type: Object,
      required: null
    }
  },
  data() {
     const validCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error("报警值不能为空"));
      } else if (!this.isvalidCode(value)) {
        callback(new Error("报警值格式不对（只能是数字）"));
      } else {
        callback();
      }
    };
    return {
      types: [
        {
          value: "voltage",
          label: "电压"
        },
        {
          value: "current",
          label: "电流"
        },
        {
          value: "power",
          label: "功率"
        },
        {
          value: "temperature",
          label: "温度"
        },
        {
          value: "pw",
          label: "瞬时功率"
        },
        {
          value: "vw",
          label: "平均功率"
        },
        {
          value: "ww",
          label: "电能"
        },
        {
          value: "ss",
          label: "功率因素"
        }
      ],
      dialog: false,
      loading: false,
      form: {
        name: "",
        value: ""
      },
      rules: {
        name: [{ required: true, message: "请选择报警类型", trigger: "blur" }],
        value: [{ required: true, validator: validCode, trigger: "blur" }]
      }
    };
  },
  created() {},
  methods: {
     isvalidCode(str) {
      const reg = /^[0-9]*$/g;
      return reg.test(str);
    },
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.dialog = true;
          if (this.is_edit) {
            _reportUpdate(
              this.form,
              res => {
                this.dialog = false;
                this.cur_this.getList();
                this.dataInit();
              },
              err => {}
            );
          } else {
            _reportCreate(
              this.form,
              res => {
                this.dialog = false;
                this.dataInit();
                this.cur_this.getList();
              },
              err => {}
            );
          }
        }
      });
    },
    dataInit() {
      this.form = {
        name: "",
        value: ""
      };
    },
    closeDialog() {}
  }
};
</script>

<style lang="scss">
</style>
